<script setup>

import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";

const user=ref(getUser());

const auditArr=ref([]);

const auditStatusList=ref([])

onMounted(()=>{
  loadDictOptions(auditStatusList,'audit_status')
  loadAudit();
})

const search=ref({
  username:'',
});

const loadAudit=()=>{
  search.value.auditUserId=user.value.id;
  search.value.auditStatus=type.value;
  let data=qs.stringify(search.value);
  axios.get(BASE_URL+'/v1/audit/select?'+data)
      .then((response)=>{
        if(response.data.code==2000){
          auditArr.value=response.data.data;
          ElMessage.success('查询成功')
        }
        else{
          ElMessage.error('查询失败')
        }
      })
};

const resetSearch=()=>{
  search.value={};
  type.value='';
  loadAudit();
};

const type=ref('');

const dialogTitle=ref('');

const auditDialogVisible=ref(false);

const auditDialog=(id)=>{
  dialogTitle.value='待审批详情';
  auditDialogVisible.value=true;
  let data=qs.stringify({id:id});
  axios.get(BASE_URL+'/v1/audit/select?'+data)
      .then((response)=>{
        if(response.data.code==2000){
          auditDialogData.value=response.data.data[0];
          ElMessage.success('查询成功')
        }
        else{
          ElMessage.error('查询失败')
        }
      })
};

const auditDialogData=ref({
  username:'',
  reason:'',
  departureAddr:'',
  destinationAddr:'',
  startTime:'',
  endTime:'',
  imgUrl:'',
  remark:'',
});

const auditPass=()=>{
  auditDialogData.value.auditStatus='30';
  let data=qs.stringify(auditDialogData.value);
  axios.post(BASE_URL+'/v1/audit/update',data)
      .then((response)=>{
        if(response.data.code==2000){
          ElMessage.success('审批成功')
          auditDialogVisible.value=false;
          auditDialogData.value={};
          loadAudit();
        }
        else{
          ElMessage.error('审批失败')
        }
      })
};

const rejectInnerDialogVisible=ref(false);

const rejectDialog=(id)=>{
  dialogTitle.value='驳回原因';
  auditDialogVisible.value=true;
  let data=qs.stringify({id:id});
  axios.get(BASE_URL+'/v1/audit/select?'+data)
      .then((response)=>{
        if(response.data.code==2000){
          auditDialogData.value=response.data.data[0];
          ElMessage.success('查询成功')
        }
        else{
          ElMessage.error('查询失败')
        }
      })
};

const rejectConfirm=()=>{
  if(confirm('确定关闭弹窗？')){
    rejectInnerDialogVisible.value=false;
    rejectReason.value='';
  }
};

const rejectReason=ref('');


const reject=()=>{
  if(!rejectReason.value || rejectReason.value.trim()==''){
    ElMessage.error('驳回原因不能为空')
    return;
  }
  auditDialogData.value.auditStatus='40';
  auditDialogData.value.rejectReason=rejectReason.value;
  let data=qs.stringify(auditDialogData.value);
  axios.post(BASE_URL+'/v1/audit/update',data)
      .then(response=>{
        if(response.data.code==2000){
          ElMessage.success('已驳回')
          rejectInnerDialogVisible.value=false;
          rejectReason.value='';
          auditDialogData.value={};
          auditDialogVisible.value=false;
          loadAudit();
        }
        else{
          ElMessage.error(response.data.msg)
        }
      })
}

</script>

<template>
  <!-- 顶部条 -->
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">审批列表</span>
  </div>
  <!-- 审批搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="用车人">
        <el-input placeholder="请输入用车人" style="width:220px;" v-model="search.username" @keydown.enter.prevent="loadAudit"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadAudit">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>

  <!-- 审批单主体 -->
  <el-card style="margin:20px;">
    <!--  审批状态项  -->
    <el-radio-group style="margin-bottom: 15px;" v-model="type" @change="loadAudit">
      <el-radio-button v-for="item in auditStatusList" :label="item.label" :value="item.value"></el-radio-button>
    </el-radio-group>
    <!--  审批列表  -->
    <el-table :data="auditArr">
      <el-table-column label="编号" prop="id" align="center" width="55" type="index"></el-table-column>
      <el-table-column label="用车人" prop="username" align="center" width="110"></el-table-column>
      <el-table-column label="开始时间" prop="startTime"  align="center"></el-table-column>
      <el-table-column label="结束时间" prop="endTime"  align="center"></el-table-column>
      <el-table-column label="用车事由" prop="reason"  align="center"></el-table-column>
      <el-table-column label="审批人" prop="auditUsernameList"  align="center"></el-table-column>
      <el-table-column label="出发地" prop="departureAddr"  align="center"></el-table-column>
      <el-table-column label="目的地" prop="destinationAddr"  align="center"></el-table-column>
      <el-table-column label="操作" width="100" align="center" v-if="type==10||type==40">
        <template #default="scope">
          <!-- 审批10：在待我审核页签下显示，需要当前登录用户进行审批 -->
          <el-button type="primary" link v-if="type==10" @click="auditDialog(scope.row.id)">审批</el-button>
          <!-- 查看40：在驳回页签下显示，可以查看已驳回申请的驳回原因 -->
          <el-button type="primary" link v-if="type==40" @click="rejectDialog(scope.row.id)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>

  <!-- 审批/查看弹窗 -->
  <el-dialog :title="dialogTitle" v-model="auditDialogVisible">
    <el-descriptions direction="horizontal" :column="2" border>
      <el-descriptions-item label="用车人">{{auditDialogData.username}}</el-descriptions-item>
      <el-descriptions-item label="用车事由">{{auditDialogData.reason}}</el-descriptions-item>
      <el-descriptions-item label="使用开始时间">{{auditDialogData.startTime}}</el-descriptions-item>
      <el-descriptions-item label="使用结束时间">{{ auditDialogData.endTime }}</el-descriptions-item>
      <el-descriptions-item label="车辆出发地">{{auditDialogData.departureAddr}}</el-descriptions-item>
      <el-descriptions-item label="车辆目的地">{{auditDialogData.destinationAddr}}</el-descriptions-item>
      <el-descriptions-item label="驾照图片">
        <img :src="BASE_URL+auditDialogData.imgUrl" style="width:150px;">
      </el-descriptions-item>
      <el-descriptions-item label="备注">{{auditDialogData.remark}}</el-descriptions-item>
      <!--  驳回原因要在点击驳回才显示 -->
        <el-descriptions-item label="驳回原因" v-if="auditDialogData.auditStatus=='40'">{{auditDialogData.rejectReason}}</el-descriptions-item>
    </el-descriptions>
    <template #footer v-if="auditDialogData.auditStatus=='10'">
      <el-button @click="auditDialogVisible=false">取消</el-button>
      <el-button type="primary" @click="rejectInnerDialogVisible=true">驳回</el-button>
      <el-button type="primary" @click="auditPass">通过</el-button>
    </template>
  </el-dialog>

  <!-- 驳回原因弹窗 -->
  <el-dialog title="驳回 查看" v-model="rejectInnerDialogVisible" style="margin-top: 37vh;" :before-close="rejectConfirm">
    <el-descriptions direction="horizontal" border>
      <el-descriptions-item label="驳回原因">
        <el-input placeholder="请输入驳回原因" v-model="rejectReason"></el-input>
      </el-descriptions-item>
    </el-descriptions>
    <template #footer>
      <el-button type="primary" plain @click="rejectConfirm">取消</el-button>
      <el-button type="primary" @click="reject">确定</el-button>
    </template>
  </el-dialog>
</template>

<style scoped>

</style>